package com.aaron.kjviewutils.car;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aaron.kjviewutils.R;
import com.aaron.kjviewutils.glide.GlideCircleTransform;
import com.aaron.kjviewutils.stock.StockInnerAdapter;
import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Aaron on 2017/3/2.
 */

public class CarsAdapter extends RecyclerView.Adapter<CarsAdapter.CarsViewHolder> {

    private List<CarBean.CarInfo.CarInner> datas;
    private Context context;
    private Activity aty;

    public CarsAdapter(Activity aty, Context context, List<CarBean.CarInfo.CarInner> datas)
    {
        this.aty = aty;
        this.context = context;
        this.datas = datas;
    }

    private StockInnerAdapter.OnItemClickListener mOnItemClickListener;

    public void setmOnItemClickListener(StockInnerAdapter.OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    @Override
    public CarsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_cars, parent, false);
        return new CarsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final CarsViewHolder holder, int position) {
        if(null!=datas&&!datas.isEmpty()&&null!=datas.get(position))
        {
            CarBean.CarInfo.CarInner carInner = datas.get(position);
            Glide.with(aty).load("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4288955613,1609665879&fm=23&gp=0.jpg").transform(new GlideCircleTransform(aty)).animate(android.R.anim.slide_in_left).placeholder(R.mipmap.ic_launcher).into(holder.carIcon);
            holder.carTag.setText(carInner.car);
            holder.carType.setText(carInner.type);
            if(null!=mOnItemClickListener)
            {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.onItemClick(v, holder.getLayoutPosition());
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return (null==datas)?0:datas.size();
    }

    public static class CarsViewHolder extends RecyclerView.ViewHolder
    {
        @Bind(R.id.carIcon)
        ImageView carIcon;
        @Bind(R.id.carTag)
        TextView carTag;
        @Bind(R.id.carType)
        TextView carType;

        public CarsViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * item点击事件
     */
    public interface OnItemClickListener {
        /**
         * item点击事件
         *
         * @param view
         * @param position
         */
        void onItemClick(View view, int position);
    }
}
